Object.defineProperty(exports, "__esModule", {
    value: !0
});

var e = require("../common/component"), t = require("../mixins/touch"), i = require("../common/utils"), n = require("../common/validator"), r = require("../common/relation");

e.VantComponent({
    mixins: [ t.touch ],
    classes: [ "nav-class", "tab-class", "tab-active-class", "line-class" ],
    relation: r.useChildren("tab", function() {
        this.updateTabs();
    }),
    props: {
        sticky: Boolean,
        border: Boolean,
        swipeable: Boolean,
        titleActiveColor: String,
        titleInactiveColor: String,
        color: String,
        animated: {
            type: Boolean,
            observer: function() {
                var e = this;
                this.children.forEach(function(t, i) {
                    return t.updateRender(i === e.data.currentIndex, e);
                });
            }
        },
        lineWidth: {
            type: null,
            value: 40,
            observer: "resize"
        },
        lineHeight: {
            type: null,
            value: -1
        },
        active: {
            type: null,
            value: 0,
            observer: function(e) {
                e !== this.getCurrentName() && this.setCurrentIndexByName(e);
            }
        },
        type: {
            type: String,
            value: "line"
        },
        ellipsis: {
            type: Boolean,
            value: !0
        },
        duration: {
            type: Number,
            value: .3
        },
        zIndex: {
            type: Number,
            value: 1
        },
        swipeThreshold: {
            type: Number,
            value: 5,
            observer: function(e) {
                this.setData({
                    scrollable: this.children.length > e || !this.data.ellipsis
                });
            }
        },
        offsetTop: {
            type: Number,
            value: 0
        },
        lazyRender: {
            type: Boolean,
            value: !0
        }
    },
    data: {
        tabs: [],
        scrollLeft: 0,
        scrollable: !1,
        currentIndex: 0,
        container: null,
        skipTransition: !0,
        lineOffsetLeft: 0
    },
    mounted: function() {
        var e = this;
        i.requestAnimationFrame(function() {
            e.setData({
                container: function() {
                    return e.createSelectorQuery().select(".van-tabs");
                }
            }), e.resize(!0), e.scrollIntoView();
        });
    },
    methods: {
        updateTabs: function() {
            var e = this.children, t = void 0 === e ? [] : e, i = this.data;
            this.setData({
                tabs: t.map(function(e) {
                    return e.data;
                }),
                scrollable: this.children.length > i.swipeThreshold || !i.ellipsis
            }), this.setCurrentIndexByName(i.active || this.getCurrentName());
        },
        trigger: function(e, t) {
            var i = this.data.currentIndex, r = t || this.children[i];
            n.isDef(r) && this.$emit(e, {
                index: r.index,
                name: r.getComputedName(),
                title: r.data.title
            });
        },
        onTap: function(e) {
            var t = this, n = e.currentTarget.dataset.index, r = this.children[n];
            r.data.disabled ? this.trigger("disabled", r) : (this.setCurrentIndex(n), i.nextTick(function() {
                t.trigger("click");
            }));
        },
        setCurrentIndexByName: function(e) {
            var t = this.children, i = (void 0 === t ? [] : t).filter(function(t) {
                return t.getComputedName() === e;
            });
            i.length && this.setCurrentIndex(i[0].index);
        },
        setCurrentIndex: function(e) {
            var t = this, r = this.data, a = this.children, s = void 0 === a ? [] : a;
            if (!(!n.isDef(e) || e >= s.length || e < 0) && (i.groupSetData(this, function() {
                s.forEach(function(i, n) {
                    var r = n === e;
                    r === i.data.active && i.inited || i.updateRender(r, t);
                });
            }), e !== r.currentIndex)) {
                var l = null !== r.currentIndex;
                this.setData({
                    currentIndex: e
                }), i.nextTick(function() {
                    t.resize(), t.scrollIntoView(), t.trigger("input"), l && t.trigger("change");
                });
            }
        },
        getCurrentName: function() {
            var e = this.children[this.data.currentIndex];
            if (e) return e.getComputedName();
        },
        resize: function(e) {
            var t = this;
            if (void 0 === e && (e = !1), "line" === this.data.type) {
                var n = this.data, r = n.currentIndex, a = n.ellipsis;
                Promise.all([ i.getAllRect(this, ".van-tab"), i.getRect(this, ".van-tabs__line") ]).then(function(i) {
                    var n = i[0], s = void 0 === n ? [] : n, l = i[1], o = s[r];
                    if (null != o) {
                        var u = s.slice(0, r).reduce(function(e, t) {
                            return e + t.width;
                        }, 0);
                        u += (o.width - l.width) / 2 + (a ? 0 : 8), t.setData({
                            lineOffsetLeft: u,
                            skipTransition: e
                        });
                    }
                });
            }
        },
        scrollIntoView: function() {
            var e = this, t = this.data, n = t.currentIndex;
            t.scrollable && Promise.all([ i.getAllRect(this, ".van-tab"), i.getRect(this, ".van-tabs__nav") ]).then(function(t) {
                var i = t[0], r = t[1], a = i[n], s = i.slice(0, n).reduce(function(e, t) {
                    return e + t.width;
                }, 0);
                e.setData({
                    scrollLeft: s - (r.width - a.width) / 2
                });
            });
        },
        onTouchScroll: function(e) {
            this.$emit("scroll", e.detail);
        },
        onTouchStart: function(e) {
            this.data.swipeable && this.touchStart(e);
        },
        onTouchMove: function(e) {
            this.data.swipeable && this.touchMove(e);
        },
        onTouchEnd: function() {
            if (this.data.swipeable) {
                var e = this.direction, t = this.deltaX, i = this.offsetX;
                if ("horizontal" === e && i >= 50) {
                    var n = this.getAvaiableTab(t);
                    -1 !== n && this.setCurrentIndex(n);
                }
            }
        },
        getAvaiableTab: function(e) {
            for (var t = this.data, i = t.tabs, n = t.currentIndex, r = e > 0 ? -1 : 1, a = r; n + a < i.length && n + a >= 0; a += r) {
                var s = n + a;
                if (s >= 0 && s < i.length && i[s] && !i[s].disabled) return s;
            }
            return -1;
        }
    }
});